package com.hdc.hdc.warehouseManage.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.shiro.web.util.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.hdc.hdc.warehouseManage.config.KVRedisConfig;
import com.hdc.hdc.warehouseManage.entity.vo.OrderVo;
import com.hdc.hdc.warehouseManage.service.InorderServiceI;
import com.hdc.hdc.warehouseManage.service.OutorderServiceI;
import com.hdc.hdc.warehouseManage.utill.AjaxReturnVO;
import com.hdc.hdc.warehouseManage.utill.OrderNumCresteUtil;

/**
 * 订单管理类
 * @author GJY
 * 人生格言：学以致用终将成功
 * @version 创建时间：2018年4月24日 下午2:22:11 
 *
 */
@RestController
@RequestMapping(value="/orderManage")
public class OrderManageController {
	
	@Autowired
	private InorderServiceI inorderServiceI;
	
	@Autowired
	private OutorderServiceI outorderServiceI;
	
	@RequestMapping(value="/inOrder")
	public ModelAndView gotoInorderView() {
		
		return new ModelAndView("/order/inorderView");
	}
	
	@RequestMapping(value="/outOrder")
	public ModelAndView gotoOutorderView() {
		
		return new ModelAndView("/order/outorderView");
	}
	
	@RequestMapping(value="/houseTohouseOrder")
	public ModelAndView gotohouseAndHouseorderView() {
		
		return new ModelAndView("/order/hAhorderView");
	}
	
	@PostMapping(value="/queryInorderDetail")
	public AjaxReturnVO queryInorderDetail(@RequestBody OrderVo vo) {
		
		AjaxReturnVO j = new AjaxReturnVO();
		
		try {
			j.setOk(true);
			j.setData(this.inorderServiceI.findInorderDetailByQuery(vo));
		} catch (Exception e) {
			e.printStackTrace();
			j.setFail(j, e.getMessage());
		}
		
		return j;
		
	}
	
	@PostMapping(value="/saveInorderForOne")
	public AjaxReturnVO insertInorder(@RequestBody OrderVo vo,HttpServletRequest request) {
		
		AjaxReturnVO j = new AjaxReturnVO();
		
		try {
			j.setOk(true);
			
			HttpSession session = WebUtils.toHttp(request).getSession();
	    	 
	    	String userinfoID = String.valueOf(session.getAttribute(KVRedisConfig.SESSION_ATTRIBUTE_USERID.getHaskey()));
	    	 
	    	String userinfoName = String.valueOf(session.getAttribute(KVRedisConfig.SESSION_ATTRIBUTE_USERNAME.getHaskey()));
			Date now = new Date();
	    	String orderNum = OrderNumCresteUtil.returnOrderNum("in");
	    	
	    	vo.setInorderNum(orderNum);
	    	vo.setCreateDate(now);
	    	vo.setCreator(userinfoID);
	    	vo.setCreatorName(userinfoName);
	    	vo.setUpdateDate(now);
	    	vo.setUpdator(userinfoID);
	    	vo.setUpdatorName(userinfoName);
			
			this.inorderServiceI.saveInorderAndGoods(vo);
		} catch (Exception e) {
			e.printStackTrace();
			j.setFail(j, e.getMessage());
		}
		
		return j;
		
	}
	
	@GetMapping(value="/inorderGoodsDetail")
	public AjaxReturnVO queryInorderGoodsDetail(@RequestParam(name="inorderNum") String inorderNum) {
		
		AjaxReturnVO j = new AjaxReturnVO();
		
		try {
			
			j.setOk(true);
			j.setData(this.inorderServiceI.findInorderGoodsDetailByInorderNum(inorderNum));
			
		} catch (Exception e) {
			e.printStackTrace();
			j.setFail(j, e.getMessage());
		}
		
		return j;
	}
	
	
	@PostMapping(value="/queryOutorderDetail")
	public AjaxReturnVO queryOutorderDetail(@RequestBody OrderVo vo) {
		
		AjaxReturnVO j = new AjaxReturnVO();
		
		try {
			
			j.setOk(true);
			j.setData(this.outorderServiceI.queryOutorderDetailByVO(vo));
		}catch (Exception e) {
			e.printStackTrace();
			j.setFail(j, e.getMessage());
		}
		
		return j;
	}
	

}
